// const { waitForDebugger } = require('inspector')
// pages/company-detail/company-detail.js
var util = require('../../utils/util.js')
// const { fileURLToPath } = require('url');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tag: '',
    personId: '',
    currentTab: 0, // tab切换
    parm: {
      name: '',
      sex: '',
      mobile: '',
      birthDay: '2020-01-01',
      liveIn: '',
      school: '',
      specialty: '',
      degree: '',
      graduateDate: '',
      returnee: '',
      exceptCity1: '',
      exceptCity2: '',
      exceptCity3: '',
      exceptCityName1: '',
      exceptCityName2: '',
      exceptCityName3: '',
      exceptDomain: '',
      exceptCompany1: '',
      exceptCompany2: '',
      exceptCompany3: '',
      fileName: '',
      exceptCompanyName1: '',
      exceptCompanyName2: '',
      exceptCompanyName3: '',
      schoolName: '',
      specialtyName: '',
      exceptCityName: '',
      exceptDomainName: '',
      flag:0,
    },
    sex: {
      data: [
        {
          value: 1,
          name: '男',
        },
        {
          value: 2,
          name: '女',
        },
      ],
      index: -1,
    },
    returnee: {
      data: [
        {
          value: 1,
          name: 'YES',
        },
        {
          value: 2,
          name: 'NO',
        },
      ],
      index: -1,
    },
    degree: {
      data: [
        {
          value: 1,
          name: '本科',
        },
        {
          value: 2,
          name: '硕士',
        },
        {
          value: 3,
          name: '博士',
        },
        {
          value: 4,
          name: '博士后',
        },
      ],
      index: -1,
    },
    specialty: {
      data: [],
      index: -1,
    },
    school: {
      data: [],
      index: -1,
    },
    exceptCity: {
      data: [],
      index: -1,
    },
    exceptDomain: {
      data: [],
      index: -1,
    },
    graduateDate: {
      data: [
        {
          value: 1,
          name: '2020年',
        },
        {
          value: 2,
          name: '2021年',
        },
        {
          value: 3,
          name: '2022年',
        },
        {
          value: 4,
          name: '2023年',
        },
        {
          value: 5,
          name: '2024年',
        },
        {
          value: 6,
          name: '2025年',
        },
      ],
      index: -1,
    },
    exceptCompanyName1: '',
    exceptCompanyName2: '',
    exceptCompanyName3: '',
    viewShowed1: false,
    viewShowed2: false,
    viewShowed3: false,
    company: [],
    carList1: [], //渲染这是的列表
    carList2: [],
    carList3: [],
    companyList: '',
    schoolList:'',
    visible: false,
    visibleSchool:false,
    company01: '',
    company02: '',
    company03: '',
    cityList: "",
    visibleCity: false,
    specialtyList:'',
    visibleSpecialty:false,
    exceptDomainVisible:false,
    visibleAll:false,
    visibleDetail:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // onReady: function(options){},
  onLoad: function (options) {
    var that = this
    var tag = options.tag
    if (tag) {
      that.setData({
        tag: tag,
      })
    }
    //获取公司列表
    util.post(app.api.getCompanys, {name:'',i:0}, (res) => {
      that.setData(
        {
          companyList: res.data,
        },
        function () {
          that.setData({
            visible: true,
          },function(){
            that.judgeVisible()
          })
        }
      )
    }),
    //todo 判断是否授权，和用户信息是否完善，否则提示并跳转首页
    // 获取专业列表
    // util.post(app.api.getSchDic, { type: 'spec',flag: 0}, function (res) {
    //   if (res.code == '200') {
    //     that.data.specialty.data = res.data
    //     that.setData({
    //       specialtyList:res.data
    //     },function(){
    //       that.setData({
    //         visibleSpecialty:true
    //       },function(){
    //         that.judgeVisible()
    //       })
    //     })
    //   }
    // })
    // 获取学校列表
    util.post(app.api.getSchDic, { type: 'school' }, function (res) {
      if (res.code == '200') {
        that.data.school.data = res.data
        that.setData({
          schoolList:res.data
        },function(){
          that.setData({
            visibleSchool:true
          },function(){
            that.judgeVisible()
          })
        })
      }
    }),
    // 获取城市列表
    util.post(app.api.getCities, {}, function (res) {
      if (res.code == '200') {
        console.log(res)
        // res.data.map(c => c['name']=c.text)
        // that.data.exceptCity.data = res.data
        let data = []
        res.data.forEach((c) => {
          let opt = {}
          opt['value'] = c.value
          opt['name'] = c.text
          data.push(opt)
        })
        that.data.exceptCity.data = data
       //
        that.setData({
          cityList:res.data
        },()=>{
          that.setData({
            visibleCity:true,
          },function(){
            that.judgeVisible()
          })
        })
      }
    }),
    util.post(app.api.getDomains, {}, function (res) {
      if (res.code == '200') {
        let data = []
        res.data.forEach((d) => {
          let opt = {}
          opt['value'] = d.value
          opt['name'] = d.text
          data.push(opt)
        })
        that.data.exceptDomain.data = data
        // 获取细分领域
        that.setData({
          exceptDomainList:res.data,
        },function(){
          that.setData({
            exceptDomainVisible:true
          },function(){
            that.judgeVisible()
          })
        })
      }
    }),
    // 获取用户详情
    setTimeout(function(){
      util.post(app.api.getSchPersonByUnionid + '/' + app.globalData.unionid, null, function (res) {
        console.log(res)
        if(!res.data){
          that.setData({
            visibleDetail:true
          })
        }
        if (res.data) {
          let p = res.data
          that.setData({
            personId: p.id,
            parm: {
              name: p.name,
              sex: '',
              mobile: p.mobile,
              birthDay: '',
              liveIn: p.liveIn,
              school: p.school,
              specialty: p.specialty,
              degree: p.highestDegree,
              graduateDate: p.graduateDate,
              returnee: p.returnee,
              flag:p.flag,
              exceptCityName1: p.exceptCityName1 == null ? '' : p.exceptCityName1,
              exceptCityName2: p.exceptCityName2 == null ? '' : p.exceptCityName2,
              exceptCityName3: p.exceptCityName3 == null ? '' : p.exceptCityName3,
              exceptCity1: p.exceptCity1 == null ? '' : p.exceptCity1,
              exceptCity2: p.exceptCity2 == null ? '' : p.exceptCity2,
              exceptCity3: p.exceptCity3 == null ? '' : p.exceptCity3,
              exceptDomain: p.exceptDomain == null ? '' : p.exceptDomain,
              exceptCompany1: p.exceptCompany1 == null ? '' : p.exceptCompany1,
              exceptCompany2: p.exceptCompany2 == null ? '' : p.exceptCompany2,
              exceptCompany3: p.exceptCompany3 == null ? '' : p.exceptCompany3,
              fileName: p.fileName == undefined ? '' : p.fileName,
              exceptCompanyName1: p.exceptCompanyName1 == null ? '' : p.exceptCompanyName1,
              exceptCompanyName2: p.exceptCompanyName2 == null ? '' : p.exceptCompanyName2,
              exceptCompanyName3: p.exceptCompanyName3 == null ? '' : p.exceptCompanyName3,
              schoolName: p.schoolName == null ? '' : p.schoolName,
              specialtyName: p.specialtyName == null ? '' : p.specialtyName,
              exceptCityName: p.exceptCityName == null ? '' : p.exceptCityName,
              exceptDomainName: p.exceptDomainName == null ? '' : p.exceptDomainName,
            },
            sex: {
              data: [
                { value: 1, name: '男' },
                { value: 2, name: '女' },
              ],
              index: p.sex - 1,
            },
            degree: {
              data: [
                { value: 1, name: '本科' },
                { value: 2, name: '硕士' },
                { value: 3, name: '博士' },
                { value: 4, name: '博士后' },
              ],
              index: p.highestDegree - 1,
            },
            graduateDate: {
              data: [
                { value: 1, name: '2020年' },
                { value: 2, name: '2021年' },
                { value: 3, name: '2022年' },
                { value: 4, name: '2023年' },
                { value: 5, name: '2024年' },
                { value: 6, name: '2025年' },
              ],
              index: p.graduateDate - 1,
            },
            returnee: { ...that.data.returnee, index: p.returnee - 1 },
            specialty: { ...that.data.specialty, index: p.specialty - 1 },
            school: { ...that.data.school, index: p.school - 1 },
            exceptCity: {
              ...that.data.exceptCity,
              index: p.exceptCity == undefined ? -1 : p.exceptCity - 1,
            },
            exceptDomain: { ...that.data.exceptDomain, index: p.exceptDomain - 3 },
          },function(){
            this.getSpecialty()
            that.setData({
              visibleDetail:true
            })
          })
        }
      })
    },100)
  },
  // 显示页面
  judgeVisible: function(){
    let exceptDomainVisible = this.data.exceptDomainVisible
    let visible = this.data.visible
    let visibleSchool = this.data.visibleSchool
    let visibleCity = this.data.visibleCity
    let visibleSpecialty = this.data.visibleSpecialty
    let visibleDetail = this.data.visibleDetail
    console.log(exceptDomainVisible,visible,visibleSchool,visibleCity,visibleSpecialty,visibleDetail)
    if(exceptDomainVisible && visible && visibleSchool && visibleCity && visibleSpecialty &&visibleDetail){
      this.setData({
        visibleAll:true
      })
    }
  },
  // 存储信息
  saveCompany01: function (e) {
    this.data.parm['exceptCompanyName1'] = e.detail.name
  },
  saveCompany02: function (e) {
    this.data.parm['exceptCompanyName2'] = e.detail.name
  },
  saveCompany03: function (e) {
    this.data.parm['exceptCompanyName3'] = e.detail.name
  },
  saveSchool: function(e){
    this.data.parm['school'] = e.detail.name
  },
  saveCity: function (e) {
      this.data.parm['liveIn'] = e.detail.name
  },
  saveCity1: function (e) {
      this.data.parm['exceptCity1'] = e.detail.id
      this.data.parm['exceptCityName1'] = e.detail.name
  },
  saveCity2: function (e) {
      this.data.parm['exceptCity2'] = e.detail.id
      this.data.parm['exceptCityName2'] = e.detail.name
  },
  saveCity3: function (e) {
      this.data.parm['exceptCity3'] = e.detail.id
      this.data.parm['exceptCityName3'] = e.detail.name
  },
  saveSpecialty: function(e){
      this.data.parm['specialty'] = e.detail.id
      this.data.parm['specialtyName'] = e.detail.name
  },
  saveExceptDomain: function(e){
      this.data.parm['exceptDomain'] = e.detail.id
      this.data.parm['exceptDomainName'] = e.detail.name
  },
  // 获取专业列表
  getSpecialty(){
    let that = this
    util.post(app.api.getSchDic, { type: 'spec',flag: that.data.parm.flag}, function (res) {
      if (res.code == '200') {
        that.data.specialty.data = res.data
        that.setData({
          specialtyList:res.data
        },function(){
          that.setData({
            visibleSpecialty:true
          },function(){
            that.judgeVisible()
          })
        })
      }
    })
  },
  getCompanys() {
    var that = this
    return new Promise((resolve, reject) => {
      util.post(
        app.api.getSchCompanys,
        {
          name: '',
        },
        function (res) {
          if (res.code === '200') {
            that.setData(
              {
                company: res.data,
              },
              () => {
                resolve()
              }
            )
          }
        }
      )
    })
  },
  // 隐藏搜索框样式
  hideInput: function () {
    this.setData({
      inputVal: '',
      viewShowed: false,
    })
  },
  // 键盘抬起事件2
  inputTyping1: function (e) {
    var value = e.detail.value
    var that = this
    var all = that.data.company
    if (value == '') {
      that.setData({
        viewShowed1: false,
      })
    } else {
      if (e.detail.cursor) {
        //e.detail.cursor表示input值当前焦点所在的位置
        let arr1 = []
        for (var i = 0; i < all.length; i++) {
          if (all[i]['name'].indexOf(value) >= 0) {
            // let x = {};
            // x['name'] = all[i]['name'];
            // x['value'] = all[i]['id'];
            // arr.push(JSON.stringify(x));
            arr1.push(all[i]['name'])
          }
        }
        that.setData({
          viewShowed1: true,
          carList1: arr1,
        })
      }
    }
  },
  inputTyping2: function (e) {
    var value = e.detail.value
    var that = this
    var all = that.data.company
    if (value == '') {
      that.setData({
        viewShowed2: false,
      })
    } else {
      if (e.detail.cursor) {
        //e.detail.cursor表示input值当前焦点所在的位置
        let arr2 = []
        for (var i = 0; i < all.length; i++) {
          if (all[i]['name'].indexOf(value) >= 0) {
            // let x = {};
            // x['name'] = all[i]['name'];
            // x['value'] = all[i]['id']
            // arr.push(JSON.stringify(x));
            arr2.push(all[i]['name'])
          }
        }
        that.setData({
          viewShowed2: true,
          carList2: arr2,
        })
      }
    }
  },
  inputTyping3: function (e) {
    var value = e.detail.value
    var that = this
    var all = that.data.company
    if (value == '') {
      that.setData({
        viewShowed3: false,
      })
    } else {
      if (e.detail.cursor) {
        //e.detail.cursor表示input值当前焦点所在的位置
        let arr3 = []
        for (var i = 0; i < all.length; i++) {
          if (all[i]['name'].indexOf(value) >= 0) {
            // let x = {};
            // x['name'] = all[i]['name'];
            // x['value'] = all[i]['id']
            // arr.push(JSON.stringify(x));
            arr3.push(all[i]['name'])
          }
        }
        that.setData({
          viewShowed3: true,
          carList3: arr3,
        })
      }
    }
  },
  // 获取选中推荐列表中的值
  name1: function (res) {
    var that = this
    var index = res.currentTarget.dataset.index
    that.setData({
      exceptCompanyName1: that.data.carList1[index],
      viewShowed1: false,
    })
  },
  name2: function (res) {
    var that = this
    var index = res.currentTarget.dataset.index
    that.setData({
      exceptCompanyName2: that.data.carList2[index],
      viewShowed2: false,
    })
  },
  name3: function (res) {
    var that = this
    var index = res.currentTarget.dataset.index
    that.setData({
      exceptCompanyName3: that.data.carList3[index],
      viewShowed3: false,
    })
  },
  bindPickerChange(e) {
    let data = this.data[e.target.dataset.name]
    let pick_index = e.detail.value
    data['index'] = pick_index
    this.setData({
      ...this.data,
      ...data,
      parm: {
        ...this.data.parm,
      },
    })
    this.data.parm[e.target.dataset.name] = data.data[data.index].value
  },
  bindDateChange(e) {
    let dataset = e.currentTarget.dataset
    this.data.parm[dataset.name] = e.detail.value
  },
  inputeidt(e) {
    let dataset = e.currentTarget.dataset
    this.data.parm[dataset.item] = e.detail.value
  },
  saveBase: function (e) {
    var that = this
    let id = this.data.personId
    let parm = this.data.parm
    let err = ''
    if (!parm.name) {
      err = '请填写姓名'
      // } else if (!parm.sex) {
      //   err = '请选择性别'
    } else if (!/^1[0-9]{10}$/.test(parm.mobile)) {
      err = '请输入正确的手机号'
    } else if (!parm.school) {
      err = '请选择学校'
    } else if (!parm.specialty) {
      err = '请选择专业'
    } else if (!parm.degree) {
      err = '请选择最高学历'
    } else if (!parm.graduateDate) {
      err = '请选择毕业年份'
    }
    if (err) {
      wx.showToast({
        title: err,
        icon: 'none',
      })
      return false
    } else {
      let uri = ''
      parm['highestDegree'] = parm.degree
      if (id) {
        parm['id'] = id
        uri = app.api.updateSchPersonBase
      } else {
        parm['unionid'] = app.globalData.unionid
        uri = app.api.addSchPersonBase
      }
      util.post(uri, parm, function (res) {
        if (res.code == '200') {
          that.setData({
            personId: res.data,
            // currentTab: 1
          })
          wx.showToast({
            title: '保存成功',
            icon: 'none',
          })
          setTimeout(() => {
            that.setData({
              currentTab: 1,
            })
          }, 1500)
        }
      })
    }
  },
  saveExcept: function () {
    let that = this
    let id = this.data.personId
    let parm = this.data.parm
    let err = ''
    if (!id) {
      err = '请先完善基本信息'
    } else if (!parm.exceptCityName1&&!parm.exceptCityName2&&!parm.exceptCityName3) {
      err = '请选择期望工作地点'
    } else if (!parm.exceptDomain) {
      err = '请选择期望加入领域'
    } else if (!parm.exceptCompanyName1&&!parm.exceptCompanyName2&&!parm.exceptCompanyName3) {
      err = '请填写志愿公司'
    }else if(parm.exceptCityName1==parm.exceptCityName2||parm.exceptCityName1==parm.exceptCityName3||parm.exceptCityName2==parm.exceptCityName3){
      err = '期望地点不能一致'
      if(!parm.exceptCityName1&&!parm.exceptCityName2||!parm.exceptCityName1&&!parm.exceptCityName3||!parm.exceptCityName2&&!parm.exceptCityName3){
        err = ""
      }
    }else if(parm.exceptCompanyName1==parm.exceptCompanyName2||parm.exceptCompanyName1==parm.exceptCompanyName3||parm.exceptCompanyName2==parm.exceptCompanyName3){
      err = '期望公司不能一致'
      if(!parm.exceptCompanyName1&&!parm.exceptCompanyName2||!parm.exceptCompanyName1&&!parm.exceptCompanyName3||!parm.exceptCompanyName2&&!parm.exceptCompanyName3){
        err = ""
      }
    }
    if (err) {
      wx.showToast({
        title: err,
        icon: 'none',
      })
      return false
    } else {
      let uri = app.api.updateSchPersonExcept
      parm['id'] = id
      parm['highestDegree'] = parm.degree
      util.post(uri, parm, function (res) {
        if (res.code == '200') {
          wx.showToast({
            title: '保存成功',
            icon: 'none',
          })
          wx.redirectTo({
            url: '/pages/schJob/index',
          })
        }
      })
    }
  },
  uploadFile: function () {
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        const tempFile = res.tempFiles[0]
        let uri = 'http://localhost:8000/tnl-wxapp/' + app.api.uploadFile2Qiniu
        wx.uploadFile({
          url: uri,
          filePath: tempFile.path,
          name: tempFile.time + '_' + tempFile.name,
          formData: {
            id: app.globalData.unionid,
          },
          success(res) {
            console.log(res)
          },
          fail(res) {
            console.log(res)
          },
        })
      },
    })
  },
  //分享
  onShareAppMessage: function (res) {
    var that = this
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    var title = that.data.companyInfo.name
    return {
      title: title,
      path: 'pages/company-detail/company-detail?id=' + that.data.companyInfo.id,
      data: that.data.companyInfo,
      success: function (res) {
        // 转发成功
        // console.log(res);
      },
      fail: function (res) {
        // console.log(res)
        // 转发失败
      },
    }
  },
  //点击tab切换
  swichNav: function (e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false
    } else {
      this.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  //
  tempTurn: function (e) {
    if(e.target.dataset.val==2){
      this.data.parm['flag'] = 1
    }else{
      this.data.parm['flag'] = 0
    }
    this.getSpecialty()
    this.setData({
      tag:e.target.dataset.val
    })
    // wx.reLaunch({
    //   url: '/pages/schPerson/index?tag=' + e.target.dataset.val,
    // })
  },
})
